package com.ww.word.ui;

import java.util.ArrayList;
import java.util.List;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.view.View;
import android.widget.AdapterView;
import android.widget.GridView;
import android.widget.AdapterView.OnItemClickListener;

import com.ww.core.activity.MyActivity;
import com.ww.word.R;
import com.ww.word.adapter.StoreAdapter;
import com.ww.word.db.StudyDbHelper;
import com.ww.word.entity.Word;

public class HistoryViewActivity extends MyActivity {
	private List<Word> list = new ArrayList<Word>();
	private GridView grid;
	private StoreAdapter storeAdapter;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_history_view);
		setTitle(getIntent().getStringExtra("time"));
		findView();

		showProgressDialog(HistoryViewActivity.this);
		new Thread(new Runnable() {
			@Override
			public void run() {
				getData();
				handler.sendEmptyMessage(1);
			}
		}).start();

	}

	private Handler handler = new Handler() {
		public void handleMessage(android.os.Message msg) {
			if (storeAdapter == null) {
				storeAdapter = new StoreAdapter(HistoryViewActivity.this, list);
				grid.setAdapter(storeAdapter);
			} else {
				storeAdapter.setList(list);
				storeAdapter.notifyDataSetChanged();
			}
			hideProgressDialog();
		}
	};

	private void getData() {
		StudyDbHelper h = new StudyDbHelper(this);
		list = h.getHistoryList(getIntent().getStringExtra("time"));
	}

	private void findView() {
		grid = (GridView) findViewById(R.id.grid);
		grid.setOnItemClickListener(new OnItemClickListener() {
			@Override
			public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
				Intent intent = new Intent();
				intent.setClass(HistoryViewActivity.this, ResultActivity.class);
				intent.putExtra("time",  list.get(arg2));
				intent.putExtra("from", "history");
				intent.putExtra("index",arg2);
				startActivity(intent);
			}
		});
	}

}
